package com.derbysoft.ota.application.translator.rs;

import java.math.BigInteger;

import com.derbysoft.modules.eip.translator.Translator;
import com.derbysoft.ota.application.util.Calendars;
import com.derbysoft.ota.application.util.ParagraphTypes;
import com.derbysoft.ota.interfaces.protocol._2009a.RatePlanType;
import com.derbysoft.remote.dswitch.hotel.dto.AvailRoomStayDTO;
import com.derbysoft.remote.dswitch.hotel.dto.CancelPolicyDTO;
import com.derbysoft.remote.dswitch.hotel.dto.RatePlanDTO;
import com.derbysoft.remote.share.dto.DateRangeDTO;

public class RatePlanTranslator implements Translator<AvailRoomStayDTO, RatePlanType> {

	@Override
	public RatePlanType translate(AvailRoomStayDTO from) {
		RatePlanType dest = new RatePlanType();
        RatePlanDTO ratePlan = from.getRatePlan();
		dest.setRatePlanCode(ratePlan.getCode());
        if(from.getQuantity() != null) {
        		dest.setAvailableQuantity(BigInteger.valueOf(from.getQuantity()));
        }
        dest.setBookingCode(ratePlan.getBookingCode());
        DateRangeDTO dateRange = ratePlan.getDateRange();
        if(dateRange != null) {
	        	dest.setEffectiveDate(Calendars.of(dateRange.getStart()));
	        	dest.setExpireDate(Calendars.nextDayOf(dateRange.getEnd()));
	        	dest.setExpireDateExclusiveIndicator(Boolean.TRUE);
        }
        dest.setRatePlanDescription(ParagraphTypes.of(ratePlan.getDescription()));
        dest.setRatePlanName(ratePlan.getName());
        com.derbysoft.remote.dswitch.hotel.constants.RatePlanType type = ratePlan.getRatePlanType();
        if (type != null) {
        		dest.setRatePlanType(type.getCode());
        }
        
        CancelPolicyDTO cancelPolicy = ratePlan.getCancelPolicy();
		if (cancelPolicy != null) {
        		dest.setCancelPenalties(new CancelPenaltiesTranslator().translate(cancelPolicy));
        }
        
        return dest;
	}

}
